package com.far.game.xiuzhen.common.utils.json;

import com.alibaba.fastjson.JSONObject;
import com.far.game.xiuzhen.common.utils.json.test.Staff;
import com.far.game.xiuzhen.common.utils.json.test.Student;
import com.far.game.xiuzhen.common.utils.json.test.Teacher;

import java.io.InputStream;
import java.util.*;

/**
 * json文件-->JSONObject转换工具
 */
public class PropDataUtil {

    public PropDataUtil(){}

    /**
     * 初始化单个目录
     * @param dirName 目录名
     * @return 文件包装器
     */
    public static FileWrapper init(String dirName) {
        return new FileWrapper(PropDataUtil.TransformJsonPropData(dirName));
    }

    /**
     * 初始化多个目录
     * @param dirNames 多个目录
     * @return 文件工厂对象
     */
    public static FileFactory init(String... dirNames) {
        Map<String, FileWrapper> fileWrapperMap = new HashMap<>();
        for (String dirName : dirNames) {
            fileWrapperMap.put(dirName,new FileWrapper(PropDataUtil.TransformJsonPropData(dirName)));
        }
        return new FileFactory(fileWrapperMap);
    }

    /**
     * 通过目录名，获取PropData对象
     * @param dirName 目录名
     * @return PropData,包含List<String> keys,Map<String, JSONObject> keyObj;
     */
    private static PropData TransformJsonPropData(String dirName){

        if (dirName.isEmpty()) {
            throw new RuntimeException("目录名为空~~~");
        }
        InputStream dir = Thread.currentThread().getContextClassLoader().getResourceAsStream(dirName);

        if (dir == null) {
            throw new RuntimeException(dirName + "下没有json文件");
        }

        List<String> jsonFileNameList = new ArrayList<>();

            try(Scanner scanner = new Scanner(dir)) {
                while (scanner.hasNextLine()) {
                    jsonFileNameList.add(scanner.nextLine());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        if (jsonFileNameList.isEmpty()) {
            throw new RuntimeException(dirName + "下没有json文件");
        }

        Map<String,JSONObject> jsonObjectMap = new HashMap<>();

        for (String jsonFileName : jsonFileNameList) {
            InputStream jsonFile = Thread.currentThread().getContextClassLoader().getResourceAsStream(dirName +"/"+ jsonFileName);
            if (jsonFile != null) {
                try(Scanner scanner = new Scanner(jsonFile)) {
                    while (scanner.hasNextLine()) {
                        JSONObject jsonObject = JSONObject.parseObject(scanner.nextLine());
                        jsonObjectMap.put(jsonFileName,jsonObject);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
        return new PropData(jsonFileNameList, jsonObjectMap);
    }


    public static void main(String[] args) {

        FileFactory fileFactory = PropDataUtil.init("school","company");

        FileWrapper companyWrapper = fileFactory.getFileWrapper("company");
        Staff staff = companyWrapper.getObj(Staff::new);
        System.out.println(staff);

        FileWrapper schoolWrapper = fileFactory.getFileWrapper("school");
        Student student = schoolWrapper.getObj(Student::new);
        System.out.println(student);

        Teacher teacher = schoolWrapper.getObj(Teacher::new);
        System.out.println(teacher);

        System.out.println();
        System.out.println();
        PropData schoolPropData = schoolWrapper.getPropData();
        System.out.println(schoolPropData.getKeys());
        System.out.println(schoolPropData.getKeyMap());

    }
}
